package com.woniu.service.Impl;

import com.woniu.dao.TestPaperDao;
import com.woniu.dao.TestQuestionDao;
import com.woniu.service.SearchBankService;
import com.woniu.service.SearchPaperService;
import com.woniuxy.commons.entity.TestPaper;
import com.woniuxy.commons.entity.TestQuestion;
import com.woniuxy.commons.enums.TestBankEnum;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @program: bzeducation
 * @author: Xingqilin
 * @create: 2020-07-20 15:45
 * @description:
 */
@Service
public class SearchPaperServiceImpl implements SearchPaperService {

    @Resource
    TestPaperDao testPaperDao;
    @Resource
    TestQuestionDao testQuestionDao;
    @Resource
    SearchBankService searchBankService;

    @Override
    @Cacheable(value = "getPaperById", unless = "#result == null")
    public TestPaper getPaperById(Long paperID) {
        TestPaper testPaper = testPaperDao.getPaperById(paperID);
        assemblePaper(testPaper);
        return testPaper;
    }

    @Override
    @Cacheable(value = "getPaperByName", unless = "#result == null ")
    public TestPaper getPaperByName(String pName) {
        TestPaper paperByName = testPaperDao.getPaperByName(pName);
        assemblePaper(paperByName);
        return paperByName;
    }

    @Override
    public List<TestPaper> getPaperList(String subject) {
        List<TestPaper> paperList = testPaperDao.getPaperList(subject);
        return paperList;
    }

    //    组装试卷
    @Override
    public void assemblePaper(TestPaper testPaper) {
        List<TestQuestion> testQuestion = testQuestionDao.getTestQuestionById(testPaper.getPaperId());
        for (TestQuestion question : testQuestion) {
            for (TestBankEnum testBankEnum : TestBankEnum.values()) {
                if (testBankEnum.getIndex().equals(question.getPaperQuestionBank())) {
                    question.setPaperDetailQuestion(
                            searchBankService.findBankById(testBankEnum, question.getPaperQuestionQid()));
                }
            }
        }
        testPaper.setPaperQuestions(testQuestion);
    }
}
